'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require("underscore");
const moment = require("moment")
const electricPriceType = {
    "1":"尖",
    "2":"峰",
    "3":"平",
    "4":"谷",
}

module.exports = async function (params) {
    try {
        let result = await conn(`
        SELECT
            cpf.cost_plan_id AS costPlanId,
            cpf.cost_plan_name AS costPlanName,
            cpf.device_type AS deviceType,
            cpf.cost_plan_type AS costPlanType,
            cpf.cost_plan_settlement AS costPlanSettlement,
            cpc.cost_plan_column_id AS costPlanColumnId,
            cpc.cost_plan_column_name AS costPlanColumnName,
            cpc.start_range AS startRange,
            cpc.end_range AS endRange,
            cpc.type,
            cpc.price
        FROM
        cost_plan_file cpf
        INNER JOIN cost_plan_column cpc ON cpf.cost_plan_id = cpc.cost_plan_id
        WHERE
        cpf.delete_sign = 1
        AND cpf.device_type = ${params.deviceType}
        AND cpf.domain_id = ${params.userDomainId}`)
        let columnGroup = _.groupBy(result,"costPlanId");
        let costPlanList = [];
        for(const costPlanId in columnGroup){
            let planColumnList = columnGroup[costPlanId];
            let planObj = {
                costPlanId:costPlanId,
                costPlanName:planColumnList[0].costPlanName,
                deviceType:planColumnList[0].deviceType,
                costPlanType:planColumnList[0].costPlanType,
                isMonth:planColumnList[0].costPlanSettlement <= 31,
                costPlanSettlement:planColumnList[0].costPlanSettlement,
                costPlanColumnList:[],
                costPlanColumnInfo:``,
            }
            for(const column of planColumnList){
                let costPlanColumnObj = {
                    costPlanColumnId:column.costPlanColumnId,
                    costPlanColumnName:column.costPlanColumnName,
                    price : column.price
                };
                if(planColumnList[0].costPlanType === 1){
                    planObj.costPlanColumnInfo = `${column.costPlanColumnName}:${column.price}`;
                    planObj.lostPrice = column.price;
                    planObj.lostPriceName = column.costPlanColumnName;
                }else{
                    if(!column.startRange && !column.endRange){
                        planObj.costPlanColumnInfo += `${column.costPlanColumnName}:${column.price} `
                        planObj.lostPrice = column.price;
                        planObj.lostPriceName = column.costPlanColumnName;
                    }else{
                        if(planColumnList[0].costPlanType === 2){
                            costPlanColumnObj.type = column.type;
                            costPlanColumnObj.startRange = new Date(`2024-03-20 ${column.startRange}`);
                            costPlanColumnObj.endRange = new Date(`2024-03-20 ${column.endRange}`);
                            planObj.costPlanColumnInfo += `${column.costPlanColumnName} ${column.startRange}-${column.endRange} ${column.price} ${electricPriceType[`${column.type}`]?electricPriceType[`${column.type}`] : ""}
                            `
                          }else{
                            costPlanColumnObj.startRange = column.startRange;
                            costPlanColumnObj.endRange = column.endRange;
                            planObj.costPlanColumnInfo += `${column.costPlanColumnName} ${column.startRange}-${column.endRange} ${column.price}
                            `

                        }
                        planObj.costPlanColumnList.push(costPlanColumnObj);
                    }

                }
            }
            costPlanList.push(planObj);
        }
        return {success: true,dataList:costPlanList};
    }catch (e){
        return {success:false,message:e.message}
    }
};